<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Http\Models\Aptitude;
use App\Http\Models\Business;

class AptitudeController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {   
        $search = $request->input('search',''); //搜索条件，没有则默认为空
        $count = $request ->input('count',10); //每页显示几条数据，默认为10
        $request = $request->all();
        $custom = Aptitude::where('ap_status',0)->where('ap_name','like','%'.$search.'%')->paginate($count);
        return view('admin.lei.aptitude.index',['custom'=>$custom,'request'=>$request]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {   
        $data = $request->except('_token');
        $apt = Aptitude::find($data['id']);
        if($apt->ap_status==0){
            $apt->ap_status = 1;
        }
        $res = $apt->save();
        $bus = new Business();
        $bus->b_name = $data['business'];
        $bus->aptitude_id = $data['id'];
        $res = $bus->save();
        if($res){
            return redirect('admin/aptitude');
        }else{
            return back();
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $apt = Aptitude::find($id);
        if($apt->ap_status==0){
            $apt->ap_status = 2;
        }
        $res = $apt->save();
        if($res){
            return redirect('admin/aptitude');
        }else{
            return back();
        }
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $data = [];
        $re =  Aptitude::find($id)->delete();
        //删除成功
        if($re){
             $data['status']= 0;
             $data['msg']='删除成功';
        }else{
            $data['status']= 1;
            $data['msg']='删除失败';
        }
       return $data;
    }

    public function trash(Request $request){
        $search = $request->input('search',''); //搜索条件，没有则默认为空
        $count = $request ->input('count',10); //每页显示几条数据，默认为10
        $request = $request->all();
        $custom = Aptitude::where('ap_status',2)->where('ap_name','like','%'.$search.'%')->paginate($count);
        return view('admin.lei.aptitude.trash',['custom'=>$custom,'request'=>$request]);
    }

    public function detail(){
        $id = $_GET['id'];
        $apt = Aptitude::find($id);
        return view('admin.lei.aptitude.detail',compact('apt'));
    }

    public function recover(){
        $id = $_GET['id'];
        $apt = Aptitude::find($id);
        if($apt->ap_status==2){
            $apt->ap_status = 0;
        }
        $res = $apt->save();
        if($res){
            return redirect('/trash');
        }else{
            return back();
        }
    }

    public function fail(){

        $id = $_GET['id'];
        $apt = Aptitude::find($id);
        if($apt->ap_status==0){
            $apt->ap_status = 2;
        }
        $res = $apt->save();
        if($res){
            return redirect('/admin/aptitude');
        }else{
            return back();
        }
    } 
}
